package com.example.newbiechen.ireader.service.boluoxs;

import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by robrk on 2019/3/10.
 */

public class ArticleCallBack implements Callback {
    private ArticleService articleService;
    public ArticleCallBack(ArticleService articleService) {
        this.articleService = articleService;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        Log.d("OkHttpClient", "onFailure: ");
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        byte[] b = response.body().bytes(); //获取数据的bytes
        String html = new String(b, "GBK"); //然后将其转为gbk

        String str = HtmlHelper.getArticleText(html);
        System.out.println(str);

        if(str.contains("    本章未完，点击下一页继续阅读》》")){
            str = str.replace("\n\n    本章未完，点击下一页继续阅读》》\n", "");
            this.articleService.getContent().append(str);
            // 获取下一页
            this.articleService.getNextPage(html);
        }else {
            // 已获取完
            this.articleService.getContent().append(str);
            this.articleService.save();
        }


//        Log.d("OkHttpClient", "onResponse: " + html);
    }
}
